package com.nbufe.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.nbufe.domain.vo.news.News;
import com.nbufe.service.NewsService;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author luzhiheng
 * @Date 2023/12/24 17:45
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Override
    public List<News> queryNews(Integer page, Integer size) {
        String url = String.format("https://www-api.carbonstop.net/api/v1/news/carbonstop_news/?language=0&news_type__code=211d650c-703b-476d-ac00-5e2975da7476&page=%d&page_size=%d", page, size);
        String jsonResponse = HttpUtil.createGet(url)
                .execute()
                .body();

        JSONObject jsonObject = JSONUtil.parseObj(jsonResponse).getJSONObject("data");
        return jsonObject.getBeanList("results", News.class);
    }

    @Override
    public List<News> queryPolicy(Integer page, Integer size) {
        String url = String.format("https://www-api.carbonstop.net/api/v1/news/carbonstop_news/?news_type__code=11623d38-06fc-4656-8a69-e0863f8cfd98&page=%d&page_size=%d", page, size);
        String jsonResponse = HttpUtil.createGet(url)
                .execute()
                .body();

        JSONObject jsonObject = JSONUtil.parseObj(jsonResponse).getJSONObject("data");
        return jsonObject.getBeanList("results", News.class);
    }

    @Override
    public List<News> queryOriginGoods(Integer page, Integer size) {
        String url = String.format("https://www-api.carbonstop.net/api/v1/news/carbonstop_news/?news_type__code=5c9422cd-f7da-42ca-b5f1-8a4375e3e2db&page=%d&page_size=%d", page, size);
        String jsonResponse = HttpUtil.createGet(url)
                .execute()
                .body();

        JSONObject jsonObject = JSONUtil.parseObj(jsonResponse).getJSONObject("data");
        return jsonObject.getBeanList("results", News.class);
    }

}
